package com.zimmur.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.common.AppModuleEnum;
import com.zimmur.dao.goods.IGoodsDao;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.base.AdBanner;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.GoodsExample;
import com.zimmur.model.goods.GoodsImage;
import com.zimmur.model.goods.GoodsParameter;
import com.zimmur.model.goods.GoodsRecommand;
import com.zimmur.model.goods.GoodsType;
import com.zimmur.model.goods.view.GoodsTypeView;
import com.zimmur.model.zone.RecommendAd;
import com.zimmur.model.zone.RecommendHomeGoods;
import com.zimmur.model.zone.RecommentAdList;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.view.IndexShow;
import com.zimmur.service.IIndexService;
import com.zimmur.service.IMakerService;
import com.zimmur.service.IUserService;
import com.zimmur.service.base.IAdBannerService;
import com.zimmur.service.base.ISystemconfigService;
import com.zimmur.service.goods.IGoodsImageService;
import com.zimmur.service.goods.IGoodsOrganizationService;
import com.zimmur.service.goods.IGoodsRecommandService;
import com.zimmur.service.goods.IGoodsService;
import com.zimmur.service.goods.IGoodsTypeService;
import com.zimmur.service.zone.ITopicMessageRecommandService;
import com.zimmur.service.zone.ITopicPictureService;
import com.zimmur.service.zone.ITopicService;
import com.zimmur.solr.service.ISolrGoodsService;
import com.zimmur.tools.PageUtil;

@Service("indexService")  
public class IndexServiceImpl implements IIndexService{
	@Resource  
	private IGoodsDao goodsDao; 

    @Resource  
    private ITopicService topicService;
    @Resource  
    private ITopicPictureService topicPictureService;
    @Resource  
    private ITopicMessageRecommandService topicMessageRecommandService;
    @Resource  
    private IGoodsService goodsService;

    @Resource   
    private IMakerService  makerService;
    @Resource  
    private IGoodsImageService goodsImageService;
    @Resource  
    private IGoodsRecommandService goodsRecommandService;
    @Resource  
    private IGoodsTypeService goodsTypeService;
    
    @Resource  
    private IUserService  userService;
    
	@Resource
	private IAdBannerService abBannerService;
	
	@Resource
	private IGoodsOrganizationService goodsOrganizationService;
	@Resource
	private ISystemconfigService systemconfigService;
	@Resource
	private ISolrGoodsService solrGoodsService;
	
    private static Logger log=LoggerFactory.getLogger(IndexServiceImpl.class);

	@Override
	public IndexShow index() {
		IndexShow indexShow = new IndexShow();
		try {
			//1.首页轮播广告 (10=公用模块(首页)/40=衣匠模块/60=家定模块)
			List<AdBanner> adBannerList = abBannerService.selectAdList(10);
			if(!adBannerList.isEmpty() && adBannerList.size() > 0){
				indexShow.setAdBannerList(adBannerList);
			}
			//2.商品类型 信息 
			/*Short typeFlag=1;//类别：1=服装/2=配饰
			List<GoodsType> goodsTypeList =goodsTypeService.selectByTypeFlagGoodsType(typeFlag);
			if(!goodsTypeList.isEmpty() && goodsTypeList.size() > 0){
				indexShow.setGoodsTypeList(goodsTypeList);
			}*/
			List<GoodsTypeView>  viewList = new ArrayList<GoodsTypeView>();
			List<GoodsType> goodsTypeList =  goodsTypeService.selectAllHomeType();
			if(goodsTypeList!=null&&!goodsTypeList.isEmpty()){
				for(GoodsType goodsType:goodsTypeList){
					if(goodsType.getParentTypeId()==0){
						List<GoodsType> subgoodsTypeList = goodsTypeService.selectByParentTypeId(goodsType.getTypeId());
						if(subgoodsTypeList!=null&&!subgoodsTypeList.isEmpty()){
							GoodsTypeView v = new GoodsTypeView();
							v.setGoodsType(goodsType);
							v.setChildrenType(subgoodsTypeList);
							viewList.add(v);
						}
					}else{//非一级分类
						GoodsTypeView v = new GoodsTypeView();
						v.setGoodsType(goodsType);
						viewList.add(v);
						
					}
					
				}
			}
			indexShow.setGoodsTypeList(viewList);
			//3.推荐广告列表(推荐顶部并排文字广告/推荐大图广告/推荐并排广告/底部推荐并排广告/推荐商品)
			RecommentAdList recAdList = new RecommentAdList();
			//推荐顶部并排文字广告
			List<RecommendAd> recommendtoptwo = abBannerService.getRecommendTopTwoAd();
			//推荐大图广告
			RecommendAd recommendbig = abBannerService.getRecommendbigAd();
			//推荐并排广告
			List<RecommendAd> recommendtwo = abBannerService.getRecommendtwoAd();
			//底部推荐并排广告
			List<RecommendAd> recommendthree = abBannerService.getRecommendthreeAd();
			//推荐商品
			List<SortClause> orderByClause=new ArrayList<>();
			orderByClause.add(new SortClause("iOrderNum", ORDER.desc));
			List<com.zimmur.solr.model.Goods> recommendHomeGoods = solrGoodsService.queryRecommendIndexGoods(4,orderByClause);
			recAdList.setRecommendbig(recommendbig);
			if(recommendtoptwo!=null&&!recommendtoptwo.isEmpty()){
				recAdList.setRecommendtoptwo(recommendtoptwo);;
			}
			if(recommendtwo!=null&&!recommendtwo.isEmpty()){
				recAdList.setRecommendtwo(recommendtwo);
			}
			if(recommendthree!=null&&!recommendthree.isEmpty()){
				recAdList.setRecommendthree(recommendthree);
			}
			recAdList.setRecommendHomeGoods(recommendHomeGoods);
			indexShow.setRecommentAdList(recAdList);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		
		return indexShow;
	}
	@Override
	public PageUtil<Topic> indexPage(long createUser, Integer pageNow) {
		PageUtil<Topic> page = new PageUtil<>();
		try {
			if(pageNow == 0){
				pageNow=1;
			}
			page = topicService.indexTopicList(createUser, pageNow,AppModuleEnum.Global);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}
	@Override
	public GoodsExample workIndex(GoodsParameter goodsParameter, long createUser, Integer pageNow) {
		GoodsExample goodsExample = new GoodsExample();
		try {
			//轮播图，广告信息:10=公用模块/20=账号管理/30=社区模块/40=衣匠模块/50=订单模块/60=家定模块
			List<AdBanner> adBannerList = abBannerService.selectAdList(40);
			if(!adBannerList.isEmpty() && adBannerList.size() > 0){
				goodsExample.setAdBannerList(adBannerList);
			}
			//显示类别、、根据上一层typeID查询ParenTypeId
			List<GoodsType> goodsTypeList = makerService.allGoodsType(goodsParameter);
			if(!goodsTypeList.isEmpty() && goodsTypeList.size() > 0){
				goodsExample.setGoodsTypeList(goodsTypeList);
			}
			//推荐商品
			List<Goods> goodsList=new ArrayList<>();
			if(pageNow == 0){
				pageNow=1;
			}
			PageUtil<Goods> page = new PageUtil<>();
			Integer totalRecord = goodsRecommandService.selectCountGoodsRecommand();
			if(totalRecord > 0){
				page.setTotalRecord(totalRecord);
				page.setPageNo(pageNow);
				goodsExample.setPage(page);
				//参数为分页条件
				List<GoodsRecommand> goodsRecommandList= goodsRecommandService.selectGoodsRecommandList(goodsParameter);
				if(!goodsRecommandList.isEmpty() && goodsRecommandList.size() > 0){
					List<Long> goodsIds=new ArrayList<>();
					for(GoodsRecommand goodsRecommandItem :goodsRecommandList){
						goodsIds.add(goodsRecommandItem.getGoodsId());
					}
					if(!goodsIds.isEmpty() && goodsIds.size() > 0){
						//商品信息
						List<Goods> goodsListS = goodsService.selectByGoodsIds(goodsIds);
						for(Goods goodsItem : goodsListS){
							//商品图片
							List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
							goodsItem.setGoodsImages(goodsImageList);
							goodsList.add(goodsItem);
						}
					}
					goodsExample.setGoodss(goodsList);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return goodsExample;
	}

	@Override
	public PageUtil<Goods> goodsTypeQuryGoodsLis(GoodsParameter goodsParameter, Integer pageNow) {
		PageUtil<Goods> page = new PageUtil<>();
		List<Goods> goodsList =new ArrayList<>();
		try {
			if(pageNow == 0){
				pageNow=1;
			}
//			page.setPageNo(pageNow);
			goodsParameter.setPageNo(pageNow);
			/*根据条件查询总数量*/
			Integer totalRecord = 0;
			
			/**
			 * 按类别类型（1衣匠2家定）根据0层类别ID查询商品
			 */
			if(goodsParameter.getTypeId() > 0 && goodsParameter.getTypeFlag() > 0 && goodsParameter.getParentTypeId() == 0){
				//按商品类型查询出商品 
				List<GoodsType> goodsTypeListS= goodsTypeService.selectByParentTypeIdGoodsType(goodsParameter);
				if(!goodsTypeListS.isEmpty() && goodsTypeListS.size() > 0){
					List<String> typeIds=new ArrayList<>();
					for(GoodsType goodsTypeItem :goodsTypeListS){
						typeIds.add(goodsTypeItem.getTypeId().toString());
					}
//					totalRecord = typeIds.size();
					goodsParameter.setTypeIds(typeIds);
					goodsParameter.setPageNo(pageNow);
					List<Goods> goodsListS = goodsService.selectListByTypeIds(goodsParameter);
					totalRecord = goodsListS.size();
					for(Goods goodsItem : goodsListS){
						//商品图片
						List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
						goodsItem.setGoodsImages(goodsImageList);
						//商品创建人信息
						GoodsUser user =userService.selectByUserId(goodsItem.getUserId());
				 		if(user != null){
				 			goodsItem.setUser(user);
				 		}
						goodsList.add(goodsItem);
					}
				}
			}
			/**
			 * 上一层ID不为0的时候，就按1层typeId查询商品
			 */
			if(goodsParameter.getTypeId() > 0 && goodsParameter.getTypeFlag() > 0 && goodsParameter.getParentTypeId() > 0){
				totalRecord = goodsService.selectByQueryCount(goodsParameter);
				List<Goods> goodsListS = goodsService.selectBytypeFlagAndTypeIdList(goodsParameter);
				for(Goods goodsItem : goodsListS){
					//商品图片
					List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
					goodsItem.setGoodsImages(goodsImageList);
					//商品创建人信息
					GoodsUser user =userService.selectByUserId(goodsItem.getUserId());
			 		if(user != null){
			 			goodsItem.setUser(user);
			 		}
					goodsList.add(goodsItem);
				}
			}
			/**
			 *全部，不按分类查询出所有商品
			 */
			//按分类类别查询所有分类（1衣匠2家定）约定分类ID为0时查询全部默认为0
			if(goodsParameter.getTypeId() == -1 && goodsParameter.getTypeFlag() > 0){
				totalRecord = goodsDao.selectCountAllGoodsDesiger();
				List<Goods> goodsListS = goodsDao.selectListBytypeFlag(goodsParameter);
				for(Goods goodsItem : goodsListS){
					//商品图片
					List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
					goodsItem.setGoodsImages(goodsImageList);
					//商品创建人信息
					GoodsUser user =userService.selectByUserId(goodsItem.getUserId());
			 		if(user != null){
			 			goodsItem.setUser(user);
			 		}
			 		goodsList.add(goodsItem);
				}
			}
			/**
			 * 按商品标题查询出商品
			 */
			if(goodsParameter.getTitle() != null && goodsParameter.getTitle() != ""){
				totalRecord = goodsService.selectByQueryCount(goodsParameter);
				List<Goods> goodsListS = goodsDao.selectGoodsListByQuery(goodsParameter);
				if(goodsListS.size() > 0 && !goodsListS.isEmpty()){
					for(Goods goodsItem : goodsListS){
						//商品图片
						List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
						goodsItem.setGoodsImages(goodsImageList);
						//商品创建人信息
						GoodsUser user =userService.selectByUserId(goodsItem.getUserId());
				 		if(user != null){
				 			goodsItem.setUser(user);
				 		}
						goodsList.add(goodsItem);
					}
				}
			}
			page.setTotalRecord(totalRecord);
			if(!goodsList.isEmpty() && goodsList.size() > 0){
				page.setItemList(goodsList);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}

	

}
